<template>
  <div class="dialog-container">
    <el-dialog
      title="用户信息"
      :visible.sync="dialogFormVisible"
      @open="onOpen"
      @opened="onOpened"
      @close="onClose"
      @closed="onClosed"
    >
      <div class="form-container">
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          label-position="top"
        >
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item
                label="编码"
                prop="code"
              >
                <el-input
                  v-model="form.code"
                  size="mini"
                  autocomplete="off"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item
                label="名称"
                prop="name"
              >
                <el-input
                  v-model="form.name"
                  size="mini"
                  autocomplete="off"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="状态">
                <el-radio-group v-model="form.state">
                  <el-radio :label="''">启用</el-radio>
                  <el-radio :label="'2'">禁用</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item
                label="密码"
                prop="password"
              >
                <el-input
                  v-model="form.password"
                  size="mini"
                  autocomplete="off"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button size="mini" @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          size="mini"
          @click="onSubmit"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      // ----------------------弹框部分-------------------------
      loading: false,
      dialogFormVisible: false,
      form: {
        id: 0,
        code: '', // 编码
        name: '', // 名称
        state: '', // 状态
        password: '' // 密码
      },
      rules: {
        code: {
          required: true,
          message: '请输入编码',
          trigger: 'blur'
        },
        name: {
          required: true,
          message: '请输入名称',
          trigger: 'blur'
        },
        password: {
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }
      }
    }
  },
  created() {},
  methods: {
    onOpen() {
      // 重置表单内容
      this.resetForm()
      // 重置表单验证
      this.resetFields('form')
      console.log(this.type, this.rowData)
    },
    resetFields(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields()
      })
    },
    resetForm() {
      this.$nextTick(() => {
        this.form = {
          id: 0,
          code: '', // 编码
          name: '', // 名称
          state: '', // 状态
          password: '' // 密码
        }
      })
    },
    onOpened() {
      if (this.type === 'add') {
        // 无动作
      } else {
        if (this.rowData && this.rowData.id) {
          for (var key in this.form) {
            this.form[key] = this.rowData[key]
          }
        }
      }
    },
    onClose() {},
    onClosed() {
      this.$parent.rowData = {}
    },
    onSubmit() {
      const formName = 'form'
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.saveFormdata()
        }
      })
    },
    saveFormdata() {
      if (!this.form.id) {
        this.$store.dispatch('system/user/userAdd', this.form).then((data) => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.dialogFormVisible = false
          this.$parent.onSearch()
        })
      } else {
        this.$store.dispatch('system/user/userEdit', this.form).then((data) => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.dialogFormVisible = false
          this.$parent.onSearch()
        })
      }
    }
  }
}
</script>
